/*
 * Copyright 2011 OpenAjax Alliance
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* ---------------------------------------------------------------- */
/*                  Initialize namespace properties                 */
/* ---------------------------------------------------------------- */

/**
 * @namespace OAA_CI
 */

var OAA_CI = OAA_CI || {};

/**
 * @namespace OAA_CI.color
 */

OAA_CI.color = OAA_CI.color || {};

OAA_CI.color.listView = new OAA_CI.SidebarView('ID_TREECHILDREN_COLOR_CONTRAST');
OAA_CI.color.views = [ OAA_CI.color.listView ];

OAA_CI.color.hideMeter = function () { OAA_CI.color.meter.hidden = true; };
OAA_CI.color.showMeter = function () { OAA_CI.color.meter.hidden = false; };

OAA_CI.sidebar = OAA_CI.color;

/**
 * getDisplayProperties
 *
 * @desc Add getDisplayProperties method to cache elements;
 *       called by populateTreeAs... functions.
 *
 * @return List of property values to be displayed in the sidebar
 */

OpenAjax.a11y.cache.ColorContrastItem.prototype.getDisplayProperties = function () {

  var cache_nls = OpenAjax.a11y.cache_nls;

  var number_of_elements = this.dom_text_nodes.length;
  
  var is_large_font = cache_nls.getBooleanNLS(this.is_large_font);

  var has_background_image = cache_nls.getBooleanNLS(this.background_image != "none");
  
  var color_contrast_summary = this.getColorContrastSummary();

  return [number_of_elements, this.color, this.background_color, is_large_font, has_background_image, this.color_contrast_ratio, color_contrast_summary];
};

/**
 * getChildren
 *
 * @desc Add getChildren method to cache elements; called by
 *       populateTreeAsHierarchy function.
 *
 * @return List of child elements
 */

OpenAjax.a11y.cache.ColorContrastItem.prototype.getChildren = function () {
  return this.dom_text_nodes;
};

/**
 * @function onLoad
 *
 * @memberOf OAA_CI.color
 *
 * @desc Initialize sidebar and populate its default tabpanel.
 */

OAA_CI.color.onLoad = function () {

  var config = {
   rules      : true,
   attributes : false,
   properties : false,
   style      : true,
   events     : false
  };

  var pt = new OAA_CI.Properties.PropertiesTabPanel("ID_PROPERTIES", config);
  OAA_CI.color.listView.addProperties(pt);  
  
  OAA_CI.color_nls_strings = document.getElementById("ID_STRINGBUNDLE_COLOR_CONTRAST");
  OAA_CI.color.meter = document.getElementById('ID_METER_COLOR_CONTRAST');

  OAA_CI.color.listView.isInitialized = false;
  OAA_CI.color.listView.showCacheItems();

};

/**
 * @function listView.showCacheItems
 *
 * @memberOf OAA_CI.color
 *
 * @desc Respond to calls in the onLoad and updateView functions. If the
 *       panel has not been initialized, populate it with color contrast
 *       items structured as a two-level list. Update properties display
 *       and page highlighting based on selection state of panel.
 */

OAA_CI.color.listView.showCacheItems = function () {
  var view = OAA_CI.color.listView;

  if (!view.isInitialized) {
    // Save references
    view.tab   = { selected: true };
    view.tree  = document.getElementById('ID_TREE_COLOR_CONTRAST');
    view.cache = OAA_CI.ruleset.dom_cache.color_contrast_cache;

    // Populate the tabpanel
    view.cache.sortCCRItems(true);
    var color_contrast_items = view.cache.color_contrast_items;
    OAA_CI.populateTreeAsTwoLevelList(color_contrast_items, 'ID_TREECHILDREN_COLOR_CONTRAST');
    view.isInitialized = true;
  }

  view.updateItemSelectionDisplay();
};

/**
 * @function updateSampleText
 *
 * @memberOf OAA_CI.color
 *
 * @desc Update the sample text based on a cache items style properties
 *
 * @param {Object}  cache_item  - The cache item to use to update the sample text
 */

OAA_CI.color.updateSampleText = function (cache_item) {

  var no_selection_message = "no item selected";
  var sample_text = document.getElementById('ID_SAMPLE_TEXT');
  var bg_image_message = document.getElementById('ID_BG_IMAGE_NOTE');
  var text;

  OpenAjax.a11y.console("Sample Text: " + cache_item.getText());

  if (cache_item && cache_item.getText) {
  
    text = cache_item.getText();
    
    if (text.length > 25) {
      text = text.substring(0,25);
      text += "...";
    }  
    
    if (text && text.length) 
      sample_text.value = text;
    else
      sample_text.value = "Sample Text";
      
    sample_text.style.color            = cache_item.computed_style.color;
    sample_text.style.backgroundColor  = cache_item.computed_style.background_color;
    sample_text.style.fontFamily       = cache_item.computed_style.font_family;
    sample_text.style.fontSize         = cache_item.computed_style.font_size;
    sample_text.style.fontWeight       = cache_item.computed_style.font_weight;

    if (cache_item.computed_style.background_image == 'none') {
      bg_image_message.value = "no background image";
      bg_image_message.style.color = "black";
    }
    else {
      bg_image_message.value = "IMPORTANT: Background image is NOT included as part of the sample text.";
      bg_image_message.style.color = "#3300CC";
    }

  }
  else {
    sample_text.value = no_selection_message;  
    sample_text.style.color            = "black";
    sample_text.style.backgroundColor  = "white";
    sample_text.style.fontFamily       = "serif";
    sample_text.style.fontSize         = "14";
    sample_text.style.fontWeight       = "100";
    
    bg_image_message.value = "no background image";
    bg_image_message.style.color = "black";
  }
  
 
};


/**
 * @function onItemSelectionChange
 *
 * @memberOf OAA_CI.color
 *
 * @desc Handle the onselect event from the XUL tree in the main tab panel
 *       by calling the updateItemSelectionDisplay method.
 *
 * @param {Number} index - Specifies the view that triggered the onselect event.
 */

OAA_CI.color.onItemSelectionChange = function (index) {

  var view = OAA_CI.color.views[index];
  var item = view.updateItemSelectionDisplay();
  
  // Update sample text
  OAA_CI.color.updateSampleText(item);
  
};
